package com.lovo.Atm;

import com.lovo.Util;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Get extends JPanel implements ActionListener {
    private JButton backImg;
    private JButton getImg;
    private Home frame;
    private JTextField printGet;
    private JLabel note;
    private Icon getBtn = Util.iconImg("/images/buttonGet.jpg");
    private Icon backBtn = Util.iconImg("/images/buttonOff.jpg");

    public Get(Home frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        //取款提示
        note = new JLabel("请输入取款金额：");
        note.setBounds(300, 230, 200, 30);
        note.setFont(new Font("宋体", Font.BOLD, 20));
        this.add(note);

        //取款输入文本框
        printGet = new JTextField();
        printGet.setBounds(600, 230, 200, 30);
        printGet.setFont(new Font("宋体", Font.BOLD, 20));
        this.add(printGet);

        //取款按钮
        getImg = new JButton(getBtn);
        getImg.setBounds(350, 300, 140, 50);
        this.add(getImg);
        getImg.addActionListener(this);

        //返回按钮，菜单界面
        backImg = new JButton(backBtn);
        backImg.setBounds(650, 300, 140, 50);
        this.add(backImg);
        backImg.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (backImg == e.getSource()) {
            frame.switchPanel("menu");
        }
        if (getImg == e.getSource()) {
            //取款金额
            double b = Double.valueOf(printGet.getText());
            //原本的金额
            double a = frame.getLoginUser().getBlance();
            if (b>a){
                JOptionPane.showMessageDialog(this,"您的余额不足，请重新输入取款金额");
                //清空输入的数据
            }else{
                JOptionPane.showMessageDialog(this,"取款成功！");
                frame.getFindPanel().set(a-b);
                //清空输入的数据
            }
            printGet.setText("");
            frame.switchPanel("menu");
        }
    }
}
